/**
 * AsyncParallelHook 异步并行钩子
 * 
 * tapable库中，有三种注册方式 tap -同步注册  tapAsync-异步注册，接受第二个参数cb  tapPromise-一个参数Promise
 */
// const { AsyncParallelHook } = require('tapable');
const { AsyncParallelHook } = require('../real-tapable');


class Hook {
    constructor() {
        this.hooks = {
            arch: new AsyncParallelHook(['arg1'])
        }
    }
    // 注册监听函数
    subscribe() {
        this.hooks.arch.tapAsync('primary', (name, cb) => {
            setTimeout(() => {
                console.log('初级前端：', name);
                cb()
            }, 1000)
        })
        this.hooks.arch.tapAsync('intermediate', (name, cb) => {
            setTimeout(() => {
                console.log('中级前端：', name);
                cb()
            }, 1000)
        })
        this.hooks.arch.tapAsync('senior', (name, cb) => {
            console.log('高级前端：', name);
            cb()
        })
    }
    // 执行监听函数
    publish() {
        this.hooks.arch.callAsync('yxfan', () => {
            console.log('封神啦！');
        })
    }
}

// const hook = new Hook;
// hook.subscribe();
// hook.publish();

class HookPromise {
    constructor() {
        this.hooks = {
            arch: new AsyncParallelHook(['arg1'])
        }
    }
    // 注册监听函数
    subscribe() {
        this.hooks.arch.tapPromise('primary', (name) => {
            return new Promise(resolve => {
                setTimeout(() => {
                    console.log('初级前端：', name);
                    resolve()
                }, 1000)
            })
            
        })
        this.hooks.arch.tapPromise('intermediate', (name) => {
            return new Promise(resolve => {
                setTimeout(() => {
                    console.log('中级前端：', name);
                    resolve()
                }, 1000)
            })
        })
        this.hooks.arch.tapPromise('senior', (name) => {
            return new Promise(resolve => {
                setTimeout(() => {
                    console.log('高级前端：', name);
                    resolve()
                }, 1000)
            })
        })
    }
    // 执行监听函数
    publish() {
        this.hooks.arch.promise('yxfan').then(() => {
            console.log('封神啦！');
        })
    }
}
const hook = new HookPromise();
hook.subscribe();
hook.publish();
